package jpf.nextsearch.util.validation.base;

import java.util.Collection;
import java.util.Map;

import jpf.nextsearch.base.GenericObject;


/**
 * @author <a href="mailto:jacek.pruszkowski@gmail.com">Jacek Pruszkowski</a>
 */
public abstract class GenericValidator extends GenericObject implements
		ObjectValidator {
	
	protected boolean isEmpty(Object value) {
		if (value == null) {
			return true;
		}
		if (value instanceof String) {
			if ((((String) value).trim()).length() == 0) {
				return true;
			}
			return false;
		}
		if (value instanceof Collection<?>) {
			return ((Collection<?>) value).isEmpty();
		}
		if (value instanceof Map<?,?>) {
			return ((Collection<?>) value).isEmpty();
		}
		return false;
	}
	
	public boolean isValueValid(Object value) {
		//zakladamy ze null jest zawsze niepoprawny
		if (value == null) {
			return false;
		}
		if (!supports(value.getClass())) {
			throw new ValidationNotSupportedException("ObjectValidator of type "+getClass().getName()
				+" does not support object "+value.getClass());
		}
		return isValueValidInternal(value);
	}
	
	protected abstract boolean isValueValidInternal(Object value);
	
	protected abstract boolean supports(Class<?> klass);
	

}
